package cn.stylefeng.guns.modular.basic.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicGoods;
import cn.stylefeng.guns.modular.basic.entity.BasicPosition;
import cn.stylefeng.guns.modular.basic.entity.BasicPositionmaterial;
import cn.stylefeng.guns.modular.basic.model.params.BasicPositionmaterialParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicPositionmaterialResult;
import cn.stylefeng.guns.modular.basic.service.BasicAreaService;
import cn.stylefeng.guns.modular.basic.service.BasicGoodsService;
import cn.stylefeng.guns.modular.basic.service.BasicPositionService;
import cn.stylefeng.guns.modular.basic.service.BasicPositionmaterialService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;


/**
 * 库位物料表控制器
 */
@Controller
@RequestMapping("/basicPositionmaterial")
public class BasicPositionmaterialController extends BaseController {

    private String PREFIX = "/basic/basicPositionmaterial";

    @Autowired
    private BasicPositionmaterialService basicPositionmaterialService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicAreaService basicAreaService;

    @Autowired
    private BasicGoodsService basicGoodsService;

    @Autowired
    private BasicPositionService basicPositionService;

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
		model.addAttribute("alist", basicAreaService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicPositionmaterial.html";
    }

    /**
     * 分配物料页面
     */
    @RequestMapping("toAssignMaterial")
    public String toAssignMaterial() {
        return PREFIX + "/assignMaterial.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/basicPositionmaterial_add.html";
    }

    /**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/basicPositionmaterial_edit.html";
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BasicPositionmaterialParam basicPositionmaterialParam) {
        this.basicPositionmaterialService.add(basicPositionmaterialParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BasicPositionmaterialParam basicPositionmaterialParam) {
        this.basicPositionmaterialService.update(basicPositionmaterialParam);
        return ResponseData.success();
    }

    /**
     * 分配物料编码
     */
    @RequestMapping("/assignMaterial")
    @ResponseBody
	@Transactional(rollbackFor = Exception.class)
    public ResponseData assignMaterial(Long goodsId, Long[] ids) {
    	User user = userService.getcurrentUser();
    	BasicGoods basicGoods = basicGoodsService.getById(goodsId);
    	Long deliverId = user.getDeliverId();
    	Long deptId = user.getDeptId();
    	for (Long id : ids) {
    		BasicPosition basicPosition = basicPositionService.getById(id);
    		BasicPositionmaterialParam p = new BasicPositionmaterialParam();
    		p.setPositionId(id);
    		p.setMaterialNumber(basicGoods.getGoodsNumber());
    		BasicPositionmaterialResult r = basicPositionmaterialService.findBySpec(p,null);
    		if (r!=null) {
				continue;
			}
    		p.setPositionNumber(basicPosition.getPositionNumber());
    		p.setMaterialName(basicGoods.getGoodsName());
    		p.setBigType(basicGoods.getGoodsBigtype());
    		p.setGoodsType(basicGoods.getGoodsType());
    		p.setDeliverId(deliverId);
    		p.setDeptId(deptId);
    		this.basicPositionmaterialService.add(p);
		}
        return ResponseData.success();
    }

    /**
     * 批量分配物料编码
     */
    @RequestMapping("/btnassignMaterial")
    @ResponseBody
	@Transactional(rollbackFor = Exception.class)
    public ResponseData btnassignMaterial(Long[] gids,Long[] ids) {
    	User user = userService.getcurrentUser();
    	Long deliverId = user.getDeliverId();
    	Long deptId = user.getDeptId();
    	for (Long id : gids) {
        	BasicGoods basicGoods = basicGoodsService.getById(id);
    		for (Long pid : ids) {
        		BasicPosition basicPosition = basicPositionService.getById(pid);
        		BasicPositionmaterialParam p = new BasicPositionmaterialParam();
        		p.setPositionId(pid);
        		p.setMaterialNumber(basicGoods.getGoodsNumber());
        		BasicPositionmaterialResult r = basicPositionmaterialService.findBySpec(p,null);
        		if (r!=null) {
    				continue;
    			}
        		p.setPositionNumber(basicPosition.getPositionNumber());
        		p.setMaterialName(basicGoods.getGoodsName());
        		p.setBigType(basicGoods.getGoodsBigtype());
        		p.setGoodsType(basicGoods.getGoodsType());
        		p.setDeliverId(deliverId);
        		p.setDeptId(deptId);
        		this.basicPositionmaterialService.add(p);
			}
		}
        return ResponseData.success();
    }

    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BasicPositionmaterialParam basicPositionmaterialParam) {
        this.basicPositionmaterialService.delete(basicPositionmaterialParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BasicPositionmaterialParam basicPositionmaterialParam) {
        BasicPositionmaterial detail = this.basicPositionmaterialService.getById(basicPositionmaterialParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BasicPositionmaterialParam basicPositionmaterialParam,String condition ) {
        return this.basicPositionmaterialService.findPageBySpec(basicPositionmaterialParam,condition);
    }

}


